<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <style>
        body{
            padding: 20px;
        }
        .box{
            width: 500px;
        }
    </style>
</head>
<body>
<div class="box">
    <form class="layui-form" action="" lay-filter="formTest" >
        <div class="layui-form-item">
            <label class="layui-form-label">登录名</label>
            <div class="layui-input-block">
            <input type="text" name="login_name" required  lay-verify="required|login_name" placeholder="请输入登录名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
            <input type="password" name="login_pwd" required lay-verify="required" placeholder="请输入登录密码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机</label>
            <div class="layui-input-block">
            <input type="text" name="sphone" required  lay-verify="required|phone" placeholder="请输入手机" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
            <input type="email" name="semail" required  lay-verify="required|email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<script src="../layui/layui.js"></script>
<script src="../js/ajax.js"></script>
<script>
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        
        //监听提交
        form.on('submit(formDemo)', function(data){
            // 加载动画
            var index = layer.load(2, {time: 10*1000});
   
            ajax({
                url: './php/res-admi.php',
                data: data.field,
                method: 'post',
                success: function(res){
                    // 关闭加载动画
                    layer.close(index);
                    if(res == 1){
                        layer.msg('添加管理员成功');
                    }else if(res == 0){
                        layer.alert('登录名已存在'); 
                        form.val('formTest', {login_name: ''});
                    }else if(res == 2){
                        layer.alert('该手机已被注册'); 
                        form.val('formTest', {sphone: ''});
                    }else if(res == 3){
                        layer.alert('该邮箱已被注册'); 
                        form.val('formTest', {semail: ''});
                    }
                    else{
                        layer.msg('添加失败, 请联系相关人员处理');
                    }
                }
            });
        return false;
        });
    });
</script>
</body>
</html>